package shop.controller;

import com.alibaba.nacos.api.utils.StringUtils;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import order.dto.*;
import order.entity.PageResponse;
import order.service.FoodNutritionI;
import order.service.FoodTagsI;
import order.service.OpenAiI;
import order.service.UserNutritionI;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import user.dto.UserDto;
import user.service.UserServiceI;

import java.util.List;

@RestController
@RequestMapping("/eating")
public class EatingController {

    @DubboReference(version = "1.0.0",timeout = 10000)
    private UserNutritionI userNutritionI;
    @DubboReference(version = "1.0.0",timeout = 10000)
    private FoodTagsI foodTagsI;
    @DubboReference(version = "1.0.0",timeout = 10000)
    private FoodNutritionI foodNutritionI;
    @DubboReference(version = "1.0.0",timeout = 10000)
    private OpenAiI openAiI;
    @DubboReference(version = "1.0.0",timeout = 10000)
    private UserServiceI userServiceI;

   /**
    * 根据UserId获取用户所有饮食记录,获取页数pageNum进行分页
    * */
    @GetMapping("/getUserAllNutrition")
    public PageResponse<UserNutrition> UserAllNutrition(@RequestParam("userId") int userId , @RequestParam("pageNum") int pageNum) {

        return userNutritionI.getNutrition(userId,pageNum);
    }

    /**
     * 添加用户饮食记录,自动更正为当日最新的饮食记录
     * */
    @PostMapping("/addUserNutrition")
    public Boolean addUserNutrition(@RequestBody UserNutrition userNutrition) {
     return   userNutritionI.addUserNutrition(userNutrition);

    }

    /**
     * 更新用户饮食记录
     * */
    @PutMapping("/updateUserNutrition")
    public Boolean updateUserNutrition(@RequestBody UserNutrition userNutrition) {
        return userNutritionI.updateUserNutrition( userNutrition);

    }

    /**
     * 根据foodId获取食物标签
     * */
    @GetMapping("/getFoodTags")
    public List<FoodTags> getFoodTags(@RequestParam("foodId") int foodId) {
        return foodTagsI.getFoodTogs(foodId);
    }
    /**
     * 获取所有食物信息
     * */
    @GetMapping("/getAllFoodNutrition")
    public List<FoodNutrition> getAllFoodNutrition() {
        return foodNutritionI.getAllFoodNutriton();
    }
    /**
     * 根据foodId获取食物信息
     * */
    @GetMapping("/getFoodNutrition")
    public FoodNutrition getFoodNutrition(@RequestParam("foodId") int foodId) {
        return foodNutritionI.getFoodNutritionById(foodId);
    }
    /**
     * 根据标签获取食物信息
     * */
    @GetMapping("/getFoodNutritionByTag")
    public List<FoodNutrition> getFoodNutritionByTag(@RequestParam("tag") String tag) {
        return foodTagsI.getFoodTogsByFoodTag(tag);
    }

    /**
     * 设置食物标签
     * */
    @PostMapping("/setFoodTags")
    public Boolean setFoodTags(@RequestParam("foodId") int foodId, @RequestParam("tag") String tag) {
        return foodTagsI.addFoodTag(foodId, tag);
    }
    /**
     * 根据食物id改变用户当天的营养值
     * */
    @PutMapping("/changeUserNutritionByFoodId")
    public  UserNutrition changeUserNutritionByFoodId(@RequestParam("userId") int userId, @RequestParam("foodId") int foodId) {
        Gson gson = new Gson();
        gson.toJson(userNutritionI.changeUserNutritionByFoodId(userId, foodId));
        return userNutritionI.changeUserNutritionByFoodId(userId, foodId);
    }
    /**
     * Ai聊天接口
     * */
    @GetMapping( "/chat/question")
    public String openAiChat(@RequestParam("question") String question,@RequestParam("userId") int userId) {
        if(StringUtils.isBlank(question)){
            return "Please Input";
        }
        UserDto userDto = userServiceI.getUserById(userId);
        if(userDto.getType() == 1){
            return openAiI.chat(question);
        } else if (userDto.getAICount() > 0) {
            openAiI.chat(question);
            userDto.setAICount(userDto.getAICount() - 1);
            userServiceI.updateUserAICount(userDto);
            return openAiI.chat(question);
        }
        return "你的今日的AI次数已用完";

    }

    /**
     *获取所有的标签
     * */
    @GetMapping("/getAllFoodTags")
    public List<FoodTagResponse> getAllFoodTags() {
        return foodTagsI.getAllFoodTags();
    }

    /**
     * 直接给出营养成分和用户id对该用户的每日营养进行修改
     * */
    @PutMapping("/changeUserNutritionByNutrition")
    public UserNutrition changeUserNutritionByNutrition(@RequestParam("userId") int userId, @RequestBody Nutrition nutrition) {
        return userNutritionI.changeUserNutritionByNutrition(userId, nutrition);
    }
    /**
     * 根据userId获取用户当日营养
     * */
    @GetMapping("/getUserNutritionByUserId")
    public UserNutrition getUserNutritionByUserId(@RequestParam("userId") int userId) {
        return userNutritionI.getUserNutritionByUserId(userId);
    }
    /**
     * 根据质量返回食物的营养成分
     * */
    @PostMapping("/CalulateUserNutrition")
    public FoodNutrition CalulateUserNutrition(@RequestBody AddNutrition addNutrition) {
        return foodNutritionI.CalcuFoodNutrition(addNutrition);
    }
    /**
     * 给出用户id,返回用户影饮食建议
     * */
    @GetMapping("/getUserFoodSuggestion")
    public String getUserFoodSuggestion(@RequestParam("userId") int userId) {
        return openAiI.adviced(userId);
    }
}

